package org.jetnuke.modules.weblinks;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.LinkLinkDAO;
import org.jetnuke.entities.LinksLinks;
import org.jetnuke.entities.LinksVoteData;

public class Details implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Details.class);

	int lid;

	public void setLid(int lid) {
		this.lid = lid;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		LinkLinkDAO ddm = DataAccessDriver.getInst().newLinkLinkDAO();
		LinksLinks link = ddm.retrieveByPK(lid);
		vc.put("link", link);
		List ls = DataAccessDriver.getInst().newLinkVoteDataDAO()
				.getByRatingLid(lid);

		Iterator it = ls.iterator();
		HashMap regVoteRating = new HashMap();
		HashMap anonVoteRating = new HashMap();

		HashMap rvvchartheight = new HashMap();
		HashMap avvchartheight = new HashMap();

		HashMap rvvpercent = new HashMap();
		HashMap avvpercent = new HashMap();

		Integer zero = new Integer(0);

		for (int i = 1; i <= 10; i++) {
			Integer kk = new Integer(i);
			regVoteRating.put(kk, zero);
			anonVoteRating.put(kk, zero);
		}

		int totalVotes = 0;
		int totalRegVotes = 0;
		int totalAnonVotes = 0;
		int topReg = 0;
		int bottomReg = 10;
		int totalRegSum = 0;

		int topAnon = 0;
		int bottomAnon = 10;
		int totalAnonSum = 0;

		while (it.hasNext()) {
			totalVotes++;
			LinksVoteData vote = (LinksVoteData) it.next();
			String user = vote.getRATINGUSER();
			int voteRating = vote.getRATING();
			if (voteRating < 1)
				voteRating = 1;
			if (voteRating > 10)
				voteRating = 10;

			if (user != null && user.length() > 0) {
				totalRegVotes++;
				totalRegSum += voteRating;
				Integer kk = new Integer(voteRating);
				Integer count = (Integer) (regVoteRating.get(kk));
				regVoteRating.put(kk, new Integer(count.intValue() + 1));

			} else {
				totalAnonVotes++;
				totalAnonSum += voteRating;
				Integer kk = new Integer(voteRating);
				Integer count = (Integer) (anonVoteRating.get(kk));
				anonVoteRating.put(kk, new Integer(count.intValue() + 1));
			}
		}
		if (totalRegVotes > 0) {
			for (int i = 1; i <= 10; i++) {
				Integer kk = new Integer(i);
				int count = ((Integer) regVoteRating.get(kk)).intValue();
				if (count > 0 && i > topReg)
					topReg = i;
				if (count > 0 && i < bottomReg)
					bottomReg = i;
				rvvchartheight.put(kk, new Integer(
						(int) (count * 100. / totalRegVotes)));
				rvvpercent.put(kk, new Integer(
						(int) (count * 100. / totalRegVotes)));
			}
		}
		if (totalAnonVotes > 0) {
			for (int i = 1; i <= 10; i++) {
				Integer kk = new Integer(i);
				int count = ((Integer) anonVoteRating.get(kk)).intValue();
				if (count > 0 && i > topAnon)
					topAnon = i;
				if (count > 0 && i < bottomAnon)
					bottomAnon = i;

				avvchartheight.put(kk, new Integer(
						(int) (count * 100. / totalAnonVotes)));
				avvpercent.put(kk, new Integer(
						(int) (count * 100. / totalAnonVotes)));
			}
		}
		vc.put("totalVotes", new Integer(totalVotes));
		vc.put("totalRegVotes", new Integer(totalRegVotes));
		vc.put("totalAnonVotes", new Integer(totalAnonVotes));
		vc.put("rvv", regVoteRating);
		vc.put("rvvchartheight", rvvchartheight);
		vc.put("rvvpercent", rvvpercent);
		vc.put("avv", anonVoteRating);
		vc.put("avvchartheight", avvchartheight);
		vc.put("avvpercent", avvpercent);
		if (totalRegVotes > 0) {
			vc.put("topreg", new Integer(topReg));
			vc.put("bottomreg", new Integer(bottomReg));
			vc.put("avgRU", new Double(totalRegSum * 1.0 / totalRegVotes));
		} else {
			vc.put("topreg", "");
			vc.put("bottomreg", "");
			vc.put("avgRU", "");
		}
		if (totalAnonVotes > 0) {
			vc.put("topanon", new Integer(topAnon));
			vc.put("bottomanon", new Integer(bottomAnon));
			vc.put("avgAU", new Double(totalAnonSum * 1.0 / totalAnonVotes));
		} else {
			vc.put("topanon", "");
			vc.put("bottomanon", "");
			vc.put("avgAU", "");
		}
		vc.put("anonweight", "10");// TODO the same value used in AddRating
		vc.put("finalrating", new Double(link.getLINKRATINGSUMMARY()));
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
